﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GPMS.Model;

namespace GPMS.Web.Controllers
{
    /// <summary>
    /// 毕业设计控制器
    /// </summary>
    public class GraduationProjectController : BaseController
    {
        //
        // GET: /GraduationProject/
        //
        // GET: /GraduationProject/Details/5

        public ActionResult TeacherGPList()//显示当前用户所发布的课题
        {
            Dictionary<Guid, int> dic = new Dictionary<Guid, int>();
            Guid UserID = GPMS.BLL.AccountHelper.GetLoginUserID(Request);
            var list = gpmsDb.GraduationProjects.Where(p => p.GuiderID == UserID).ToList();
            list.ForEach(i =>
            {
                dic.Add(i.GPID, gpmsDb.GPSelections.Count(g => g.GPID == i.GPID));
            });
            ViewData["dic"] = dic;
            return View(list);
        }

        public ActionResult SudentGPSList()//学生选的课题
        {
            Guid id = GPMS.BLL.AccountHelper.GetLoginUserID(Request);
            Dictionary<Guid, int> dic = new Dictionary<Guid, int>();
            Dictionary<Guid, string> guiders = new Dictionary<Guid, string>();
            var list = gpmsDb.GraduationProjects.Where(g => g.GPSelections.Any(s => s.UserID == id)).ToList();
            list.ForEach(i =>
            {
                dic.Add(i.GPID, gpmsDb.GPSelections.Count(g => g.GPID == i.GPID));
                guiders.Add(i.GPID, gpmsDb.GraduationProjects.First(g => g.GPID == i.GPID).Guider.RealName);
            });
            ViewData["dic"] = dic;
            ViewData["guider"] = guiders;
            return View(list);
        }


        //
        // GET: /GraduationProject/Create

        public ActionResult GraduationProjectCreate()
        {
            return View();
        }

        //
        // POST: /GraduationProject/Create

        [HttpPost]
        public ActionResult GraduationProjectCreate(GraduationProject project, FormCollection collection)//课题发布
        {
            try
            {
                if (gpmsDb.GraduationProjects.Any(g => g.GPName == project.GPName))
                {
                    return JsonError("已存在同名课题");
                }
                else
                {
                    Guid id = GPMS.BLL.AccountHelper.GetLoginUserID(Request);
                    project.GuiderID = id;
                    project.GPID = Guid.NewGuid();
                    project.CreateDate = DateTime.Now;
                    project.Audit = false;
                    gpmsDb.AddToGraduationProjects(project);
                    gpmsDb.SaveChanges();
                    return Success("课题发布成功");
                }
            }
            catch (Exception ex)
            {

                return JsonError("新增失败：" + ex.Message);
            }

        }

        public ActionResult AdminGPList()//管理员的课题列表
        {
            Guid specialtyID = GPMS.BLL.AccountHelper.GetLonginUserSpecialty(Request, gpmsDb);
            var list = gpmsDb.GraduationProjects.Where(g => g.Guider.SpecialtyID == specialtyID).ToList();
            Guid id = GPMS.BLL.AccountHelper.GetLoginUserID(Request);
            Dictionary<Guid, int> dic = new Dictionary<Guid, int>();
            Dictionary<Guid, string> guiders = new Dictionary<Guid, string>();
            list.ForEach(i =>
            {
                dic.Add(i.GPID, gpmsDb.GPSelections.Count(g => g.GPID == i.GPID));
                guiders.Add(i.GPID, gpmsDb.GraduationProjects.First(g => g.GPID == i.GPID).Guider.RealName);
            });
            ViewData["dic"] = dic;
            ViewData["guider"] = guiders;
            return View(list);
        }
        public ActionResult AdminAudit(Guid id)//课题通过
        {
            return Confirm(string.Format("您确定审批这个课题：{0} 吗？",gpmsDb.GraduationProjects.FirstOrDefault(g=>g.GPID==id).GPName));
        }
        [HttpPost]
        public ActionResult AdminAudit(Guid id,FormCollection collection)//课题通过
        {
            try
            {
                GraduationProject project = gpmsDb.GraduationProjects.FirstOrDefault(p => p.GPID == id);
                project.Audit = true;
                gpmsDb.SaveChanges();
                return Success("课题通过！！");
            }
            catch (Exception ex)
            {
                return JsonError("课题通过失败：" + ex.Message);
            }
        }
        //
        // GET: /GraduationProject/Edit/5

        public ActionResult GPEdit(Guid id)//课题修改
        {
            GraduationProject project = gpmsDb.GraduationProjects.FirstOrDefault(p => p.GPID == id);
            return View(project);
        }

        //
        // POST: /GraduationProject/Edit/5

        [HttpPost]
        public ActionResult GPEdit(Guid id, FormCollection collection)//课题修改
        {
            //try
            //{
                GraduationProject project = gpmsDb.GraduationProjects.FirstOrDefault(p => p.GPID == id);
                UpdateModel(project, collection);
                gpmsDb.SaveChanges();
                return Success("修改成功！！");
            //}
            //catch (Exception ex)
            //{
            //    return JsonError("修改失败：" + ex.Message);
            //}
        }


        //
        // GET: /GraduationProject/Delete/5

        public ActionResult GPDelete(Guid id)
        {
            return Confirm(string.Format("确定删除课题：{0} ?", gpmsDb.GraduationProjects.FirstOrDefault(p => p.GPID == id).GPName));
        }

        //
        // POST: /GraduationProject/Delete/5

        [HttpPost]
        public ActionResult GPDelete(Guid id, FormCollection collection)//课题删除
        {
            Guid userId = GPMS.BLL.AccountHelper.GetLoginUserID(Request);
            if (userId != gpmsDb.GraduationProjects.FirstOrDefault(g => g.GPID == id).GuiderID)
            {
                return JsonError("您不是该课题所有者，不能删除");
            }
            var userCount = (from u in gpmsDb.GPSelections where u.GPID == id select u).Count();
            if (userCount > 0)
            {
                return JsonError("该课题已有人选，不能删除");
            }
            GraduationProject project = gpmsDb.GraduationProjects.FirstOrDefault(p => p.GPID == id);
            gpmsDb.GraduationProjects.DeleteObject(project);
            gpmsDb.SaveChanges();
            return Success("删除成功");
        }
    }
}
